import os

import allure
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver

from web_ui_item.utils.logger import logger
from web_ui_item.utils.read_data import r_data

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
COOKIE_PATH = os.path.join(BASE_PATH, "data/cookie.yaml")
ENV_PATH = os.path.join(BASE_PATH, "env.ini")
ELE_PATH = os.path.join(BASE_PATH, "data/elements.yaml")


class BasePage:
    _base_url = r_data.load_ini(ENV_PATH)["main"]["url"]

    def __init__(self, base_driver=None):
        if base_driver is None:
            with allure.step("打开首页，登录"):
                # self.driver = webdriver.Chrome()
                chrome_options = Options()
                chrome_options.add_argument('--headless')
                chrome_options.add_argument('--disable-gpu')
                chrome_options.add_argument("window-size=1024,768")
                chrome_options.add_argument("--no-sandbox")
                self.driver = webdriver.Chrome(chrome_options=chrome_options)
                # 进入到对应的页面
                if self._base_url:
                    self.driver.get(self._base_url)
                    self.__cookie_login()
                    self.driver.maximize_window()
                    # 隐式等待3秒
                    self.driver.implicitly_wait(3)
        else:
            self.driver: WebDriver = base_driver

    def __cookie_login(self):
        '''
        通过cookie登录
        :return:
        '''
        try:
            cookies = r_data.load_yaml(COOKIE_PATH)
            for cookie in cookies:
                self.driver.add_cookie(cookie)
            self.driver.refresh()
        except:
            logger.debug("cookie植入异常")

    def find(self, by, locator):
        '''
        查找元素，支持传入元组
        :param by:
        :param locator:
        :return:
        '''
        if locator is None:
            return self.driver.find_element(**by)
        else:
            return self.driver.find_element(by, locator)

    def finds(self, by, locator):
        '''
        查找元素组，支持传入元组
        :param by:
        :param locator:
        :return:
        '''
        if locator is None:
            return self.driver.find_elements(**by)
        else:
            return self.driver.find_elements(by, locator)

    def close_driver(self):
        '''
        关闭浏览器
        :return:
        '''
        self.driver.quit()

    def double_click(self, by, locator):
        '''双击事件'''
        element = self.find(by, locator)
        ActionChains(self.driver).double_click(element).perform()

    BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))

    def get_test_data(self, yaml_file_name):
        path = os.path.join(BASE_PATH, "data", yaml_file_name)
        test_data = r_data.load_yaml(path)
        return test_data

    def get_page_elements(self, page):
        '''
        获取文件中的元素
        :param page:
        :return:
        '''
        elements = r_data.load_yaml(ELE_PATH)[page]
        return elements
